package com.cheng.controller;

import com.cheng.pojo.AppCategory;
import com.cheng.pojo.AppInfo;
import com.cheng.pojo.DataDictionary;
import com.cheng.pojo.DevUser;
import com.cheng.service.appcategory.AppCategoryService;
import com.cheng.service.appinfo.AppInfoService;
import com.cheng.service.datadictionary.DataDictionaryService;
import com.cheng.service.devuser.DevUserService;
import com.cheng.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@SuppressWarnings("all")
@RequestMapping(value = "/dev")
public class DevUserController {
    @Autowired
    private DevUserService devUserService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private AppInfoService appInfoService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "devlogin";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(DevUser devUser, HttpSession session, Model model) {
        DevUser user = devUserService.login(devUser);
        if (user != null) {
            session.setAttribute("devUserSession", user);
            return "developer/main";
        } else {
            model.addAttribute("msg", "账号或密码错误!");
        }
        return "devlogin";
    }

    @RequestMapping(value = "/appInfo")
    public String appInfoPage(Model model) {
        List<DataDictionary> statusList = dataDictionaryService.getAllAppStatus();
        model.addAttribute("statusList", statusList);

        List<DataDictionary> flatFormList = dataDictionaryService.getAllFlatform();
        model.addAttribute("flatFormList", flatFormList);

        List<AppCategory> categoryLevel1List = appCategoryService.getTypeLevel1();
        model.addAttribute("categoryLevel1List", categoryLevel1List);

        AppInfo appInfo = new AppInfo();
        appInfo.setSoftwareName("");

        PageUtils<AppInfo> pageUtils = new PageUtils<>();
        pageUtils.setCurrentPage(1);
        pageUtils.setPageSize(5);

        appInfoService.findAll(appInfo, pageUtils);
        model.addAttribute("page", pageUtils);

        return "developer/appinfolist";
    }
}
